//
//  PersonalViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/8/30.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "PersonalViewController.h"
#import "UserInfoViewController.h"
#import "TerminalEquipmentViewController.h"
#import "PaymentViewController.h"
#import "AlarmClockViewController.h"
#import "AboutViewController.h"
#import "LoginViewController.h"
#import "UIImage+ImageEffects.h"
#import "OrderViewController.h"
#import "MyGradesViewController.h"
#import "UIImageView+WebCache.h"
#import "CollectionViewController.h"
#import "SettingViewController.h"
#import "OpinionViewController.h"
#import "MyActivityViewController.h"
@interface PersonalViewController ()<UIAlertViewDelegate>
@property (strong, nonatomic) UIAlertView *alertView;
@property (strong, nonatomic) IBOutlet UILabel *labName;
@property (strong, nonatomic) IBOutlet UIImageView *imageUser;
@property (strong, nonatomic) IBOutlet UILabel *labIntegral;
@end

@implementation PersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    [self showNavgationWithLeftItemWithTitle:@"首页"];
    [self showNavBarWithRightImage:[UIImage imageNamed:@"settings_normol"] andRightBtnAction:@selector(rightButtonAction)];
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView setTableFooterView:v];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:UserDataID];
    if (userID == nil || [userID isEqualToString:@""] || userID == NULL) {
        self.labName.text = @"未登录";
        self.labIntegral.text = @"";
        self.imageUser.image = [UIImage imageNamed:@"icon_user-1"];
    }else{
        [self requestUserInfo];
    }
}

#pragma mark - 数据请求
- (void)requestUserInfo{
    [[BaseDataRequest alloc]getUserInfoWithBlock:^(id responseObject, NSString *status, NSError *error) {
        if ([status isEqualToString:@"0"]) {
            NSDictionary *dicResult = [responseObject objectForKey:@"result"];
            self.labName.text = [dicResult objectForKey:@"name"];
            self.labIntegral.text = [NSString stringWithFormat:@"%@ 积分",[dicResult objectForKey:@"score"]];
            [self.imageUser sd_setImageWithURL:[NSURL URLWithString:[dicResult objectForKey:@"head_url"]] placeholderImage:[UIImage imageNamed:@"icon_user-1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                if(image == nil){
                    self.imageUser.image = [UIImage imageNamed:@"icon_user-1"];
                }else{
                    self.imageUser.image = image;
                    self.imageUser.layer.borderWidth = 0;
                    self.imageUser.layer.cornerRadius = 24;
                    self.imageUser.layer.masksToBounds = YES;
                }
            }];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW(@"获取用户信息失败");
            }
            
        }
    }];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (![self logInspection]) {
            return;
        }
        if (indexPath.row == 0) {
            //用户信息
            UserInfoViewController *userInfo = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"UserInfoViewController"];
            [self.navigationController pushViewController:userInfo animated:YES];
        }
    }else if (indexPath.section == 1) {
        if (![self logInspection]) {
            return;
        }
        if(indexPath.row == 0 ){
            //我的积分
            MyGradesViewController *grades = [[MyGradesViewController alloc]init];
            [self.navigationController pushViewController:grades animated:YES];
        }else if (indexPath.row == 1) {
            //我的订单
            OrderViewController *userInfo = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderViewController"];
            [self.navigationController pushViewController:userInfo animated:YES];
        }else if (indexPath.row ==2){
            //我的收藏
            CollectionViewController *collectionVC = [[UIStoryboard storyboardWithName:@"ArticleViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"CollectionViewController"];
            [self.navigationController pushViewController:collectionVC animated:YES];
        }else{
            //我的活动
            MyActivityViewController *activity = [[MyActivityViewController alloc]init];
            [self.navigationController pushViewController:activity animated:YES];
        }
    }else if (indexPath.section == 2) {
        if (![self logInspection]) {
            return;
        }
        if (indexPath.row == 2){
            //终端设备
            TerminalEquipmentViewController *userInfo = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"TerminalEquipmentViewController"];
            [self.navigationController pushViewController:userInfo animated:YES];
        }else if (indexPath.row == 0){
            //缴费续费
            PaymentViewController *userInfo = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"PaymentViewController"];
            [self.navigationController pushViewController:userInfo animated:YES];
        }else if (indexPath.row == 1){
            //提醒与闹钟设置
            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"蓝牙功能尚在开发中，敬请期待" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alertV.tag =1;
            [alertV show];
            return;
            /*AlarmClockViewController *versionVC = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"AlarmClockViewController"];
            [self.navigationController pushViewController:versionVC animated:YES];*/
        }
    }else if(indexPath.section == 3){
        OpinionViewController *versionVC = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"OpinionViewController"];
        [self.navigationController pushViewController:versionVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 74;
    }else{
        return 50;
    }
}

#pragma mark - UIAlertViewDelehgate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 1) {
        return;
    }
    if (buttonIndex == 1) {
        LoginViewController *login = getStoryOfControllerInstance(@"LoginViewController");
        BaseNavController *baseNav = [[BaseNavController alloc] initWithRootViewController:login];
        [UIApplication sharedApplication].delegate.window.rootViewController = baseNav;
    }
}

#pragma mark set/get
- (UIAlertView *)alertView{
    if (_alertView == nil) {
        _alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录，是否立即登录" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
        _alertView.delegate = self;
    }
    return _alertView;
}

#pragma mark - 私有方法
- (BOOL )logInspection{
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:UserDataID];
    if ([str isEqualToString:@""] || str == nil || str == NULL) {
        [self.alertView show];
        return NO;
    }else{
        return YES;
    }
}

- (void)rightButtonAction{
    SettingViewController *setting = [[UIStoryboard storyboardWithName:@"Setting" bundle:nil]instantiateViewControllerWithIdentifier:@"Setting"];
    [self.navigationController pushViewController:setting animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
